************ Figure 3

**Poland
clear all
import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(B4:I8) firstrow
gen level = _n
gen tot = 0
foreach var in atOrigin inGermany inUSA inIsrael{
	destring `var', replace
	replace tot = tot + `var' if `var' != .
}
qui total tot
replace tot = tot / _b[tot]

graph twoway (line Normalized level, lcolor(gs0) lpattern(solid) ///
       ylabel(0(0.2)1.2, format(%02.1f)) ///
	   xlabel(1 `" "≤ Lower" "Secondary" "' 2 `" "Upper" "Secondary" "' 3 `" "Post-" "Secondary" "' 4 "Tertiary") ///
	   xscale(range(0.75 4.25)) ///
	   xtitle("") ytitle("Relative Probability of Migrating") ///
	   title("Poland")) ///
	   (bar tot level, yaxis(2) ytitle("", axis(2)) ylabel(0(0.1)0.6, axis(2) format(%02.1f)) barwidth(0.6) fcolor(gs12%40) lcolor(none) legend(off))
graph save "$path\Tempfiles\fig3_1.gph", replace
	   
	   
**Hungary
clear all
import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(B14:I18) firstrow
gen level = _n
gen tot = 0
foreach var in atOrigin inGermany inUSA inIsrael{
	destring `var', replace force
	replace tot = tot + `var' if `var' != .
}
qui total tot
replace tot = tot / _b[tot]

graph twoway (line Normalized level, lcolor(gs0) lpattern(solid) ///
       ylabel(0(0.2)1.2, format(%02.1f)) ///
	   xlabel(1 `" "≤ Lower" "Secondary" "' 2 `" "Upper" "Secondary" "' 3 `" "Post-" "Secondary" "' 4 "Tertiary") ///
	   xscale(range(0.75 4.25)) ///
	   xtitle("") ytitle("") ///
	   title("Hungary")) ///
	   (bar tot level, yaxis(2) ytitle("Educational Attainment Share", axis(2)) ylabel(0(0.1)0.6, axis(2) format(%02.1f)) barwidth(0.6) fcolor(gs12%40) lcolor(none) legend(off))
graph save "$path\Tempfiles\fig3_2.gph", replace

**Romania
import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(B34:I38) firstrow clear
gen level = _n
gen tot = 0
foreach var in atOrigin inGermany inUSA inIsrael{
	destring `var', replace force
	replace tot = tot + `var' if `var' != .
}
qui total tot
replace tot = tot / _b[tot]
save "$path\Tempfiles\romania_2.dta", replace

import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(B44:I48) firstrow clear
gen level = _n
gen tot = 0
foreach var in atOrigin inGermany inUSA inIsrael{
	destring `var', replace force
	replace tot = tot + `var' if `var' != .
}
qui total tot
replace tot = tot / _b[tot]
save "$path\Tempfiles\romania_3.dta", replace

import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(B24:I28) firstrow clear
gen level = _n
gen tot = 0
foreach var in atOrigin inGermany inUSA inIsrael{
	destring `var', replace force
	replace tot = tot + `var' if `var' != .
}
qui total tot
replace tot = tot / _b[tot]

gen group = "all"
append using "$path\Tempfiles\romania_2.dta", force
replace group = "jews" if group == ""
append using "$path\Tempfiles\romania_3.dta", force
replace group = "germans" if group == ""
sleep 500
erase "$path\Tempfiles\romania_2.dta"
erase "$path\Tempfiles\romania_3.dta"

graph twoway (line Normalized level if group == "all", lcolor(gs0) lpattern(solid) ///
       ylabel(0(0.2)1.2, format(%02.1f)) ///
       xlabel(1 `" "≤ Lower" "Secondary" "' 2 `" "Upper" "Secondary" "' 3 `" "Post-" "Secondary" "' 4 "Tertiary") ///
       xscale(range(0.75 4.25)) ///
       xtitle("") ytitle("Relative Probability of Migrating") ///
       title("Romania") ///
       legend(pos(6) rows(1) order(1 "Overall" 2 "Jews" 3 "Ethnic Germans" 4 "Education Distribution")) ///
       ) ///
       (line Normalized level if group == "jews", lcolor(gs6) lpattern(shortdash)) ///
       (line Normalized level if group == "germans", lcolor(gs10) lpattern(longdash)) ///
       (bar tot level if group == "all", yaxis(2) ytitle("", axis(2)) ylabel(0(0.1)0.6, axis(2) format(%02.1f)) barwidth(0.6) fcolor(gs12%40) lcolor(none) legend(off))
graph save "$path\Tempfiles\fig3_3.gph", replace

**Russia
import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(B63:I66) firstrow clear
gen level = _n
gen tot = 0
foreach var in atOrigin inGermany inUSA inIsrael{
	destring `var', replace force
	replace tot = tot + `var' if `var' != .
}
qui total tot
replace tot = tot / _b[tot]
save "$path\Tempfiles\russia_2.dta", replace

import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(B72:I75) firstrow clear
gen level = _n
gen tot = 0
foreach var in atOrigin inGermany inUSA inIsrael{
	destring `var', replace force
	replace tot = tot + `var' if `var' != .
}
qui total tot
replace tot = tot / _b[tot]
save "$path\Tempfiles\russia_3.dta", replace

import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(B54:I57) firstrow clear
gen level = _n
gen tot = 0
foreach var in atOrigin inGermany inUSA inIsrael{
	destring `var', replace force
	replace tot = tot + `var' if `var' != .
}
qui total tot
replace tot = tot / _b[tot]

gen group = "all"
append using "$path\Tempfiles\russia_2.dta", force
replace group = "jews" if group == ""
append using "$path\Tempfiles\russia_3.dta", force
replace group = "germans" if group == ""
sleep 500
erase "$path\Tempfiles\russia_2.dta"
erase "$path\Tempfiles\russia_3.dta"

graph twoway (line Normalized level if group == "all", lcolor(gs0) lpattern(solid) ///
       ylabel(0(0.4)2, format(%02.1f)) ///
	   xlabel(1 `" "≤ Lower" "Secondary" "' 2 `" "Upper" "Secondary" "' 3 `" "Post-" "Secondary" "') ///
	   xscale(range(0.75 3.25)) ///
	   xtitle("") ytitle("") ///
	   title("Russia")) ///
	   (bar tot level if group == "all", yaxis(2) ytitle("Educational Attainment Share", axis(2)) ylabel(0(0.1)0.9, axis(2) format(%02.1f)) barwidth(0.6) fcolor(gs12%40) lcolor(none) legend(off)) ///
	   (line Normalized level if group == "jews", lcolor(gs6) lpattern(shortdash)) ///
	   (line Normalized level if group == "germans", lcolor(gs10) lpattern(longdash))
graph save "$path\Tempfiles\fig3_4.gph", replace


*combine, erase tempfiles
grc1leg2 "$path\Tempfiles\fig3_1.gph" "$path\Tempfiles\fig3_2.gph" "$path\Tempfiles\fig3_3.gph" "$path\Tempfiles\fig3_4.gph", rows(2) imargin(zero) legscale(*1.1) leg("$path\Tempfiles\fig3_3.gph")
graph export "$path\Output\fig3.pdf", replace

sleep 500
erase "$path\Tempfiles\fig3_1.gph"
erase "$path\Tempfiles\fig3_2.gph"
erase "$path\Tempfiles\fig3_3.gph"
erase "$path\Tempfiles\fig3_4.gph"


************ Figure B2

**Poland
clear all
import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(B4:I8) firstrow
gen level = _n
gen tot = 0
foreach var in atOrigin inGermany inUSA inIsrael{
	destring `var', replace
	replace tot = tot + `var' if `var' != .
}
qui total tot
replace tot = tot / _b[tot]
save "$path\Tempfiles\poland_edu.dta", replace


import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(W4:AG8) firstrow clear
gen level = _n
save "$path\Tempfiles\poland_2.dta", replace

import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(AI4:AS8) firstrow clear
gen level = _n
save "$path\Tempfiles\poland_3.dta", replace


import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(K4:U8) firstrow clear
gen level = _n

gen group = "early"
append using "$path\Tempfiles\poland_2.dta", force
replace group = "late" if group == ""
append using "$path\Tempfiles\poland_3.dta", force
replace group = "econ" if group == ""
append using "$path\Tempfiles\poland_edu.dta", force
replace group = "edu" if group == ""

sleep 500
erase "$path\Tempfiles\poland_2.dta"
erase "$path\Tempfiles\poland_3.dta"
erase "$path\Tempfiles\poland_edu.dta"

local endash = ustrunescape("\u2013")
graph twoway (line Normalized level if group == "early", lcolor(gs0) lpattern(solid) ///
       ylabel(0(0.2)1.2, format(%02.1f)) ///
       xlabel(1 `" "≤ Lower" "Secondary" "' 2 `" "Upper" "Secondary" "' 3 `" "Post-" "Secondary" "' 4 "Tertiary") ///
       xscale(range(0.75 4.25)) ///
       xtitle("") ytitle("Relative Probability of Migrating") ///
       title("Poland") ///
	   legend(pos(6) rows(1) order(1 "Migrated 1970`endash'1986" 2 "Migrated 1987`endash'1992" 3 "Migrated 1993`endash'2003" 4 "Education Distribution")) ///
       ) ///
       (line Normalized level if group == "late", lcolor(gs6) lpattern(shortdash)) ///
       (line Normalized level if group == "econ", lcolor(gs10) lpattern(longdash)) ///
       (bar tot level if group == "edu", yaxis(2) ytitle("", axis(2)) ylabel(0(0.1)0.6, axis(2) format(%02.1f)) barwidth(0.6) fcolor(gs12%40) lcolor(none) legend(off))
graph save "$path\Tempfiles\figb2_1.gph", replace

	   
	   
**Hungary
clear all
import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(B14:I18) firstrow
gen level = _n
gen tot = 0
foreach var in atOrigin inGermany inUSA inIsrael{
	destring `var', replace force
	replace tot = tot + `var' if `var' != .
}
qui total tot
replace tot = tot / _b[tot]
save "$path\Tempfiles\hungary_edu.dta", replace


import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(W14:AG18) firstrow clear
gen level = _n
save "$path\Tempfiles\hungary_2.dta", replace

import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(AI14:AS18) firstrow clear
gen level = _n
save "$path\Tempfiles\hungary_3.dta", replace


import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(K14:U18) firstrow clear
gen level = _n

gen group = "early"
append using "$path\Tempfiles\hungary_2.dta", force
replace group = "late" if group == ""
append using "$path\Tempfiles\hungary_3.dta", force
replace group = "econ" if group == ""
append using "$path\Tempfiles\hungary_edu.dta", force
replace group = "edu" if group == ""

sleep 500
erase "$path\Tempfiles\hungary_2.dta"
erase "$path\Tempfiles\hungary_3.dta"
erase "$path\Tempfiles\hungary_edu.dta"

local endash = ustrunescape("\u2013")
graph twoway (line Normalized level if group == "early", lcolor(gs0) lpattern(solid) ///
       ylabel(0(0.2)1.2, format(%02.1f)) ///
       xlabel(1 `" "≤ Lower" "Secondary" "' 2 `" "Upper" "Secondary" "' 3 `" "Post-" "Secondary" "' 4 "Tertiary") ///
       xscale(range(0.75 4.25)) ///
       xtitle("") ytitle("") ///
       title("Hungary") ///
       ) ///
       (line Normalized level if group == "late", lcolor(gs6) lpattern(shortdash)) ///
       (line Normalized level if group == "econ", lcolor(gs10) lpattern(longdash)) ///
       (bar tot level if group == "edu", yaxis(2) ytitle("Educational Attainment Share", axis(2)) ylabel(0(0.1)0.6, axis(2) format(%02.1f)) barwidth(0.6) fcolor(gs12%40) lcolor(none) legend(off))
graph save "$path\Tempfiles\figb2_2.gph", replace


**Romania
clear all
import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(B4:I8) firstrow
gen level = _n
gen tot = 0
foreach var in atOrigin inGermany inUSA inIsrael{
	destring `var', replace
	replace tot = tot + `var' if `var' != .
}
qui total tot
replace tot = tot / _b[tot]
save "$path\Tempfiles\romania_edu.dta", replace


import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(W24:AG28) firstrow clear
gen level = _n
save "$path\Tempfiles\romania_2.dta", replace

import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(AI24:AS28) firstrow clear
gen level = _n
save "$path\Tempfiles\romania_3.dta", replace


import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(K24:U28) firstrow clear
gen level = _n

gen group = "early"
append using "$path\Tempfiles\romania_2.dta", force
replace group = "late" if group == ""
append using "$path\Tempfiles\romania_3.dta", force
replace group = "econ" if group == ""
append using "$path\Tempfiles\romania_edu.dta", force
replace group = "edu" if group == ""

sleep 500
erase "$path\Tempfiles\romania_2.dta"
erase "$path\Tempfiles\romania_3.dta"
erase "$path\Tempfiles\romania_edu.dta"

local endash = ustrunescape("\u2013")
graph twoway (line Normalized level if group == "early", lcolor(gs0) lpattern(solid) ///
       ylabel(0(0.4)2, format(%02.1f)) ///
       xlabel(1 `" "≤ Lower" "Secondary" "' 2 `" "Upper" "Secondary" "' 3 `" "Post-" "Secondary" "' 4 "Tertiary") ///
       xscale(range(0.75 4.25)) ///
       xtitle("") ytitle("Relative Probability of Migrating") ///
       title("Romania") ///
       ) ///
       (line Normalized level if group == "late", lcolor(gs6) lpattern(shortdash)) ///
       (line Normalized level if group == "econ", lcolor(gs10) lpattern(longdash)) ///
       (bar tot level if group == "edu", yaxis(2) ytitle("", axis(2)) ylabel(0(0.1)0.5, axis(2) format(%02.1f)) barwidth(0.6) fcolor(gs12%40) lcolor(none) legend(off))
graph save "$path\Tempfiles\figb2_3.gph", replace


**Russia
clear all
import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(B54:I57) firstrow
gen level = _n
gen tot = 0
foreach var in atOrigin inGermany inUSA inIsrael{
	destring `var', replace force
	replace tot = tot + `var' if `var' != .
}
qui total tot
replace tot = tot / _b[tot]
save "$path\Tempfiles\russia_edu.dta", replace


import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(W54:AG57) firstrow clear
gen level = _n
save "$path\Tempfiles\russia_2.dta", replace

import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(AI54:AS57) firstrow clear
gen level = _n
save "$path\Tempfiles\russia_3.dta", replace


import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(K54:U57) firstrow clear
gen level = _n

gen group = "early"
append using "$path\Tempfiles\russia_2.dta", force
replace group = "late" if group == ""
append using "$path\Tempfiles\russia_3.dta", force
replace group = "econ" if group == ""
append using "$path\Tempfiles\russia_edu.dta", force
replace group = "edu" if group == ""

sleep 500
erase "$path\Tempfiles\russia_2.dta"
erase "$path\Tempfiles\russia_3.dta"
erase "$path\Tempfiles\russia_edu.dta"

local endash = ustrunescape("\u2013")
graph twoway (line Normalized level if group == "early", lcolor(gs0) lpattern(solid) ///
       ylabel(0(0.4)2, format(%02.1f)) ///
       xlabel(1 `" "≤ Lower" "Secondary" "' 2 `" "Upper" "Secondary" "' 3 `" "Post-" "Secondary" "') ///
       xscale(range(0.75 3.25)) ///
       xtitle("") ytitle("") ///
       title("Russia") ///
       ) ///
       (line Normalized level if group == "late", lcolor(gs6) lpattern(shortdash)) ///
       (line Normalized level if group == "econ", lcolor(gs10) lpattern(longdash)) ///
       (bar tot level if group == "edu", yaxis(2) ytitle("Educational Attainment Share", axis(2)) ylabel(0(0.2)1, axis(2) format(%02.1f)) barwidth(0.6) fcolor(gs12%40) lcolor(none) legend(off))
graph save "$path\Tempfiles\figb2_4.gph", replace


*combine, erase tempfiles
grc1leg2 "$path\Tempfiles\figb2_1.gph" "$path\Tempfiles\figb2_2.gph" "$path\Tempfiles\figb2_3.gph" "$path\Tempfiles\figb2_4.gph", rows(2) imargin(zero) legscale(*0.95) leg("$path\Tempfiles\figb2_1.gph")
graph export "$path\Output\figb2.pdf", replace

sleep 500
erase "$path\Tempfiles\figb2_1.gph"
erase "$path\Tempfiles\figb2_2.gph"
erase "$path\Tempfiles\figb2_3.gph"
erase "$path\Tempfiles\figb2_4.gph"



************ Figure B2

clear all
import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(W44:AG48) firstrow
gen level = _n
save "$path\Tempfiles\romania_germans.dta", replace

import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(W63:AG66) firstrow clear
gen level = _n
replace level = 4 if level == 3
save "$path\Tempfiles\russia_jews.dta", replace

import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(W72:AG75) firstrow clear
gen level = _n
replace level = 4 if level == 3
save "$path\Tempfiles\russia_germans.dta", replace

import excel using "$path\Output\Compile_Tables.xlsx", sheet("Figs3_B2_B3") cellrange(W34:AG38) firstrow clear
gen level = _n

gen group = "romania_jews"
append using "$path\Tempfiles\romania_germans.dta", force
replace group = "romania_germans" if group == ""
append using "$path\Tempfiles\russia_jews.dta", force
replace group = "russia_jews" if group == ""
append using "$path\Tempfiles\russia_germans.dta", force
replace group = "russia_germans" if group == ""

sleep 500
erase "$path\Tempfiles\romania_germans.dta"
erase "$path\Tempfiles\russia_jews.dta"
erase "$path\Tempfiles\russia_germans.dta"

graph twoway (line PrMig level if group == "romania_jews", lcolor(gs0) lpattern(solid) ///
       ylabel(0(0.2)1, format(%02.1f)) ///
       xlabel(1 `" "≤ Lower" "Secondary" "' 2 `" "Upper" "Secondary" "' 3 `" "Post-" "Secondary" "' 4 "Tertiary") ///
       xscale(range(0.75 4.25)) ///
       xtitle("") ytitle("Probability of Migrating") ///
	   legend(pos(6) rows(1) order(1 "Romanian Jews" 2 "Romanian Germans" 3 "Soviet Jews" 4 "Soviet Germans")) ///
       ) ///
       (line PrMig level if group == "romania_germans", lcolor(gs4) lpattern(shortdash)) ///
       (line PrMig level if group == "russia_jews", lcolor(gs8) lpattern(longdash)) ///
       (line PrMig level if group == "russia_germans", lcolor(gs11) lpattern(dash_dot))
graph export "$path\Output\figb3.pdf", replace
